package com.xbongbong.pro.rabbitmq.consumer;

import com.xbongbong.paas.pojo.FieldAttrEntityForImport;
import com.xbongbong.paas.pojo.dto.ImportFormDataDTO;
import com.xbongbong.paas.pojo.dto.ImportMessageFormDataDTO;
import com.xbongbong.paas.pojo.dto.ImportOtherParamsDTO;
import com.xbongbong.paas.pojo.imports.CellTitlePojo;
import com.xbongbong.paas.pojo.imports.RowGroupPojo;
import com.xbongbong.paas.service.HandleImportService;
import com.xbongbong.paas.service.impl.PaasExcelServiceImpl;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.rabbitmq.binders.ImportConsumerBinder;
import com.xbongbong.pro.rabbitmq.constant.ImportBinderConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Async;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * crm 消息消费者
 * @author 魏荣杰
 * @date 2019/2/25 14:04
 * @since v1.0
 * @version v1.0
 */
@EnableBinding(ImportConsumerBinder.class)
public class ImportListener {
    private static Logger LOG = LoggerFactory.getLogger(ImportListener.class);
    @Resource
    private HandleImportService handleImportService;
    @Autowired
    Environment environment;
    @Resource
    private PaasExcelServiceImpl paasExcelServiceImpl;


    @StreamListener(value = ImportBinderConstant.IMPORT_ADD_INPUT)
    @Async(value = "importRabbitMqThreadPool")
    public void importReceive(ImportMessageFormDataDTO importMessageFormDataDTO ) throws  Exception {
        extracted(importMessageFormDataDTO);
    }


    @StreamListener(value = ImportBinderConstant.IMPORT_LARGE_NUM_ADD_INPUT)
    @Async(value = "importRabbitMqThreadPool")
    public void importLargeNumReceive(ImportMessageFormDataDTO importMessageFormDataDTO ) throws  Exception {
        extracted(importMessageFormDataDTO);
    }

    private void extracted(ImportMessageFormDataDTO importMessageFormDataDTO) {
        String[] split = importMessageFormDataDTO.getLocale().split("_");
        LocaleContextHolder.setLocale(new Locale(split[0], split[1]));
        try {
            Integer totalRowNum = importMessageFormDataDTO.getTotalRowNum();
            Integer totalSize = importMessageFormDataDTO.getTotalSize();
            List<ImportMessageFormDataDTO.RowGroupPojo> staticRowGroupPojoList = importMessageFormDataDTO.getRowGroupPojoList();
            List<RowGroupPojo> rowGroupPojoList = new ArrayList<>(staticRowGroupPojoList.size());
            List<ImportMessageFormDataDTO.CellTitlePojo> statictitlePojoLis = importMessageFormDataDTO.getTitlePojoLis();
            List<CellTitlePojo> titlePojoList = new ArrayList<>(statictitlePojoLis.size());
            BeanUtil.copyPropertiesList(staticRowGroupPojoList,rowGroupPojoList,RowGroupPojo.class);
            BeanUtil.copyPropertiesList(statictitlePojoLis,titlePojoList,CellTitlePojo.class);
            ImportOtherParamsDTO importOtherParamsDTO = new ImportOtherParamsDTO();
            importOtherParamsDTO.setTotalSize(totalSize);
            ImportFormDataDTO importFormDataDTO = importMessageFormDataDTO.getImportFormDataDTO();
            List<FieldAttrEntityForImport> fieldAttrEntityForImportList = paasExcelServiceImpl.getNeedQueryData(importFormDataDTO, importMessageFormDataDTO.getJsonArray(), rowGroupPojoList, titlePojoList);
            TransmittableThreadLocalUtil.setValue(importFormDataDTO.getSaasMark(), importFormDataDTO.getBusinessType(), importFormDataDTO.getMenuId(), importFormDataDTO.getDistributorMark(),
                    importFormDataDTO.getCorpid(), importFormDataDTO.getUserId(), importFormDataDTO.getFormId());
            handleImportService.importSaasData(importOtherParamsDTO, importFormDataDTO, totalRowNum, rowGroupPojoList, titlePojoList, fieldAttrEntityForImportList, importMessageFormDataDTO.getCheckRepeatKey());
        } catch (Exception e) {
            LOG.error("",e);
            //TODO 推出去
        }
    }
}
